<?php
namespace app\hongguan\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
    // 网站首页
    public function index()
    {
        // 查询导航菜单
        $web = Db::name('web')->select();
        // 查询banner图
        $banner = Db::name('zhanshi')->select();
        // 查询通知公告
        // 查询最新一个视频
        $noticeVideo = Db::name('notice')
            ->where('type', '=', 2)
            ->order('create_time', 'desc')
            ->limit(0, 1)
            ->find();
        // halt(  $noticeVideo );
        //处理日期
        $noticeVideo['date'] = date('Y-m', strtotime($noticeVideo['create_time']));
        $noticeVideo['day'] = date('d', strtotime($noticeVideo['create_time']));;
        // 查询 5 条最新的公告
        $noticeList = Db::name('notice')
            ->field('id,title,create_time')
            ->where('type', '=', 1)
            ->order('create_time', 'desc')
            ->limit(0, 5)
            ->select();
        // 查询 3 条最新的主题教育数据
        $articleEdu = Db::name('zhutijiaoyu')
            ->field('id,title,image,createtime')
            ->order('createtime', 'desc')
            ->limit(0, 3)
            ->select();
        // 查询 4 条最新的工作动态数据（不带封面图的）
        $articleWork = Db::name('gongzuodt')
            ->field('id,title,abstract,createtime')
            ->order('createtime', 'desc')
            ->where('type', '=', 1)
            ->limit(0, 4)
            ->select();
        foreach ($articleWork as $k => $v) {
            $articleWork[$k]['date'] = date('Y-m', strtotime($articleWork[$k]['createtime']));
            $articleWork[$k]['day'] = date('d', strtotime($articleWork[$k]['createtime']));
        }
        // 查询 1 条最新的工作动态数据（带封面图的）
        $articleWork2 = Db::name('gongzuodt')
            ->field('id,title,image')
            ->order('createtime', 'desc')
            ->where('type', '=', 2)
            ->limit(0, 1)
            ->find();

        // 查询 4 条最新的社会服务数据
        $articleSocial = Db::name('shehuifw')
            ->field('id,title,image,abstract,createtime')
            ->order('createtime', 'desc')
            ->where('type', '=', 2)
            ->limit(0, 4)
            ->select();
        // 处理日期时间
        foreach ($articleSocial as $k => $v) {
            $articleSocial[$k]['date'] = date('Y-m-d', strtotime($articleSocial[$k]['createtime']));
        }
        // halt($articleSocial);

        // 查询友情链接
        $friendLink = Db::name('lianjiebiao')->select();
        // 查询红馆展厅的相册
        // 小图（4张）
        $smallImages = Db::name('image')->
        where('type', '=', '2')->order('id', 'desc')
            ->limit(4)
            ->select();
        //halt($smallImages);
        // 大图（2张）
        $bigImages = Db::name('image')
            ->field('id,image')
            ->where('type', '=', '1')->order('id', 'desc')
            ->limit(0, 2)
            ->select();
        // halt($bigImages);
        return view('index', [
            'web' => $web,
            'zhanshi' => $banner,
            'noticeVideo' => $noticeVideo,
            'noticeList' => $noticeList,
            'zhutijiaoyu' => $articleEdu,
            'work' => $articleWork,
            'gongzuodtRight' => $articleWork2,
            'shehuifw' => $articleSocial,
            'lianjiebiao' => $friendLink,
            'smallImages' => $smallImages,
            'bigImages' => $bigImages]);
    }
         // 红馆简介
    public function hgintro()
    {
        // 接收搜索的关键字
        $keyword = input('keyword');
        $where = [];
        if (!empty($keyword)) {
            $where['title'] = ['like', '%'.$keyword.'%'];
        }
        // 查询导航菜单
        $nav = Db::name('web')->select();
        // 查询banner图
        $banner = Db::name('zhanshi')->select();
        // 查询 4 条最新的主题教育数据
        $articleEdu = Db::name('zhutijiaoyu')
            ->field('id,title,image,createtime')
            ->where($where)
            ->order('createtime', 'desc')
            ->limit(0, 4)
            ->select();
        return view('cardList', [
            'nav' => $nav,
            'banner' => $banner,
            'articleEdu' => $articleEdu,
        ]);
    }
}
